import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { resolve } from 'path'
import vitePluginImp from 'vite-plugin-imp'

export default defineConfig(({ mode }) => {
    // 获取各种环境下对应的变量
    const env = loadEnv(mode, process.cwd())
    return {
        plugins: [
            react(),
            vitePluginImp({
                libList: [
                    {
                        libName: 'antd',
                        style: (name: string) => `antd/es/${name}/style`
                    }
                ]
            })
        ],
        resolve: {
            alias: {
                '@': resolve(__dirname, 'src')
            }
        },
        css: {
            preprocessorOptions: {
                less: {
                    // 修改为 less
                    javascriptEnabled: true,
                    modifyVars: {
                        '@primary-color': '#4377FE' // 设置 antd 主题色
                    }
                }
            }
        },
        // 代理跨域
        server: {
            proxy: {
                [env.VITE_APP_BASE_API]: {
                    // 获取数据的服务器地址的设置
                    target: env.VITE_SERVE,
                    // 需要代理跨域
                    changeOrigin: true,
                    // 路径重写
                    rewrite: (path: string) => path.replace(/^\/api/, '')
                }
            }
        }
    }
})
